const SUCCESS = 0,
  FAIL = -1,
  ERROR = -2,
  UNAUTHORIZED = -401

class Resp {
  constructor(code, msg, data) {
    this.code = code
    this.msg = msg
    this.data = data
  }
  success(data) {
    return new Resp(SUCCESS, null, data)
  }
  fail(msg) {
    return new Resp(FAIL, msg, null)
  }
  error(msg) {
    return new Resp(ERROR, msg, null)
  }
  unauthorized() {
    return new Resp(UNAUTHORIZED, '登录已过期', null)
  }
  isSuccess() {
    return this.code === SUCCESS
  }
  isFail() {
    return this.code === FAIL
  }
  isError() {
    return this.code === ERROR
  }
  isUnauthorized() {
    return this.code === UNAUTHORIZED
  }
}

module.exports = new Resp()
